﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.UTIL;
using ESHOP.Domain;
using ESHOP.DAO;
using ESHOP.ADMIN.Common;
namespace ESHOP.ADMIN
{
    public partial class Login : Authentication
    {
        private readonly UserAdminDao _objServ = new UserAdminDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                txtuser.Focus();
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckLogin())
            {
                Response.Redirect(WebUrlConstants.HOME_PAGE, true);
            }
        }

        private bool CheckLogin()
        {
            try
            {
                if (Page.IsValid)
                {

                    var objUi = new USER_ADMIN
                    {
                        USERNAME = txtuser.Text.Trim()
                    };
                    var password = txtpassword.Text.Trim();
                    objUi.PASS = password;
                    var objDb = _objServ.Get(objUi.USERNAME,objUi.PASS);
                    if (objDb != null)
                    {
                        Session[SessionKey.UserLogin] = objDb;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return false;
            }

            return false;
        }
    }
}